﻿using System.Net.Http.Json;

namespace ImoviesBlazor.Client.Pages.admin
{
    public partial class Collect
    {
        /// <summary>
        /// 视频类型选择类
        /// </summary>
        public class SelectItem
        {
            public string Label { get; set; }
            public string Value { get; set; }

            public SelectItem(string label, string value)
            {
                this.Label = label;
                this.Value = value;
            }
        }

        /// <summary>
        /// 视频分类下拉框内容
        /// </summary>
        List<SelectItem> selectItems = new()
        {
            new SelectItem("电视剧", "电视剧"),
            new SelectItem("电影", "电影"),
            new SelectItem("动漫", "动漫")
        };

        /// <summary>
        /// 被选中的视频分类
        /// </summary>
        public string selectedVideoCate = string.Empty;

        /// <summary>
        /// 待采集url
        /// </summary>
        public string awaitCollectUrl = string.Empty;

        /// <summary>
        /// 待采集电视剧url列表
        /// </summary>
        private List<string> awaitTeleplayCollectList = new List<string>();

        /// <summary>
        /// 待采集电影url列表
        /// </summary>
        private List<string> awaitMovieCollectList = new List<string>();

        /// <summary>
        /// 待采集动漫url列表
        /// </summary>
        private List<string> awaitAnimationCollectList = new List<string>();


        /// <summary>
        /// 视频类型选择改变
        /// </summary>
        /// <returns></returns>
        public void VcateSelectedChange(string value)
        {
            selectedVideoCate = value;
        }

        /// <summary>
        /// 分类下拉框清空事件
        /// </summary>
        public void ClearVcate()
        {
            selectedVideoCate = string.Empty;
            awaitCollectUrl = string.Empty;
        }

        /// <summary>
        /// 添加url到待采集列表
        /// </summary>
        public void AddToAwaitList()
        {
            if (string.IsNullOrEmpty(awaitCollectUrl))
            {
                return;
            }
            switch (selectedVideoCate)
            {
                case "电视剧":
                    awaitTeleplayCollectList.Add(awaitCollectUrl);
                    break;
                case "电影":
                    awaitMovieCollectList.Add(awaitCollectUrl);
                    break;
                case "动漫":
                    awaitAnimationCollectList.Add(awaitCollectUrl);
                    break;
                default:
                    break;
            }

            awaitCollectUrl = string.Empty;
        }

        /// <summary>
        /// 电视剧采集等待
        /// </summary>
        private bool teleplayLoading = false;
        /// <summary>
        /// 开始电视剧采集
        /// </summary>
        private async void startTeleplayCollect()
        {
            if (awaitTeleplayCollectList.Count < 1) return;

            teleplayLoading = true;
            // 进行采集请求
            var res = await HttpClient.PostAsJsonAsync<List<string>>("api/collect/teleplay", awaitTeleplayCollectList).WaitAsync(TimeSpan.FromSeconds(10));
            if (res.IsSuccessStatusCode)
            {
                teleplayLoading = false;
                await InvokeAsync(StateHasChanged);
                awaitTeleplayCollectList.Clear();
            }
        }


        /// <summary>
        /// 电影采集等待
        /// </summary>
        private bool movieLoading = false;
        /// <summary>
        /// 开始电影采集
        /// </summary>
        private async Task startMovieCollectAsync()
        {
            if (awaitMovieCollectList.Count < 1) return;

            movieLoading = true;
            // 进行采集请求
            var res = await HttpClient.PostAsJsonAsync<List<string>>("api/collect/movie", awaitMovieCollectList).WaitAsync(TimeSpan.FromSeconds(10));
            if (res.IsSuccessStatusCode)
            {
                movieLoading = false;
                await InvokeAsync(StateHasChanged);
                awaitMovieCollectList.Clear();
            }
        }


        /// <summary>
        /// 动漫采集等待
        /// </summary>
        private bool animationLoading = false;
        /// <summary>
        /// 开始动漫采集
        /// </summary>
        private async Task startAnimationCollectAsync()
        {
            if (awaitAnimationCollectList.Count < 1) return;

            animationLoading = true;
            // 进行采集请求
            var res = await HttpClient.PostAsJsonAsync<List<string>>("api/collect/animation", awaitAnimationCollectList).WaitAsync(TimeSpan.FromSeconds(10));
            if (res.IsSuccessStatusCode)
            {
                animationLoading = false;
                await InvokeAsync(StateHasChanged);
                awaitAnimationCollectList.Clear();
            }
        }
    }
}
